provider "alicloud" {
  region                = local.region
}

terraform {
  required_version      = "~> 0.12"  
  backend "oss" {
    bucket              = "deeproute-terraform-resources"
    key                 = "stag/services/stg-ack-operation-cluster-cnsz-cnsz"
    tablestore_endpoint = "https://terraform-status.cn-shenzhen.ots.aliyuncs.com"
    tablestore_table    = "terraform_statelock_table"    
    region              = "cn-shenzhen"
  }
}

locals {
  vpc                   = "vpc-wz9nh1bbw1cbmdnknw5g7"
}

data "alicloud_instance_types" "r6-2xlarge" {
  cpu_core_count               = 8
  memory_size                  = 64
  instance_type_family         = "ecs.r6"
  kubernetes_node_role         = "Worker"
}

resource "alicloud_cs_managed_kubernetes" "main" {
  name                          = "stg-ack-operation-cluster-cnsz"
  count                         = 1
  version                       = "1.22.15-aliyun.1"
  timezone                      = "Asia/Shanghai"
  resource_group_id             = "rg-aek2tc4weboko6i"
  worker_vswitch_ids            = ["vsw-wz9dmjf3cwwiznco6fuwf"]
  node_cidr_mask                = "24"  #per node 62 pods, cluster availabel scale to 16 nodes
  proxy_mode                    = "ipvs"
  pod_cidr                      = "10.246.0.0/19"
  service_cidr                  = "10.246.32.0/22"
  cluster_spec                  = "ack.pro.small" #"ack.standard" #"ack.pro.small"
  new_nat_gateway               = false
  is_enterprise_security_group  = true
  # load_balancer_spec            = "slb.s1.small"
  slb_internet_enabled          = false

  dynamic "addons" {
      for_each = var.cluster_addons
      content {
        name                    = lookup(addons.value, "name", var.cluster_addons)
        config                  = lookup(addons.value, "config", var.cluster_addons)
        disabled                = lookup(addons.value, "disabled", var.cluster_addons)
      }
  }
  maintenance_window {
    enable            = false
    maintenance_time  = "01:00:00Z"
    duration          = "3h"
    weekly_period     = "Monday,Friday"
  }
  tags                = local.tags
}

# NodePoolName-v{a}-v{b}-v{c}
#   a: cluster version
#   b: k8s version
#   c: node group version

resource "alicloud_cs_kubernetes_node_pool" "np1" {
  cluster_id                   = alicloud_cs_managed_kubernetes.main.0.id
  name                         = "public-worker-node-v1-v1-22-v1"
  vswitch_ids                  = ["vsw-wz9dmjf3cwwiznco6fuwf"]
  instance_types               = [data.alicloud_instance_types.r6-2xlarge.instance_types.0.id]
  image_id                     = "aliyun_3_x64_20G_alibase_20220225.vhd"   //https://help.aliyun.com/document_detail/173054.html?spm=a2c4g.11186623.6.581.6c0571c8KnnMZV
  resource_group_id            = "rg-aek2tc4weboko6i"
  password                     = "Just4Test"
  system_disk_category         = "cloud_essd"
  system_disk_size             = "80"
  install_cloud_monitor        = true
  instance_charge_type         = "PostPaid"
  runtime_name                 = "docker"
  runtime_version              = "19.03.15"
  # # use PrePaid
  # instance_charge_type         = "PrePaid"
  # period                       = 1
  # period_unit                  = "Month"
  # auto_renew                   = true
  # auto_renew_period            = 1
  desired_size                  = 9 //which can be zero
  management {
    auto_repair       = false
    auto_upgrade      = false
    max_unavailable   = 1
  } 
  # # With auto-scaling is enabled, the nodes in the node pool will be labeled with `k8s.aliyun.com=true` to prevent system pods such as coredns, metrics-servers from being scheduled to elastic nodes, and to prevent node shrinkage from causing business abnormalities.
  # scaling_config {
  #   min_size          = 2
  #   max_size          = 10
  #   is_bond_eip       = false
  # }
  labels {
    key               = "env"
    value             = "staging-ack"
  }
  labels {
    key               = "cluster"
    value             = "stg-ack-operation-cluster-cnsz"
  }
  labels {
    key               = "nodePool"
    value             = "public-worker-node-v1-v1-22-v1"
  }
  tags                = local.tags
}

resource "alicloud_cs_kubernetes_node_pool" "np2" {
  cluster_id                   = alicloud_cs_managed_kubernetes.main.0.id
  name                         = "public-worker-node-v1-v1-22-v2"
  vswitch_ids                  = ["vsw-wz9dmjf3cwwiznco6fuwf"]
  instance_types               = [data.alicloud_instance_types.r6-2xlarge.instance_types.0.id]
  image_id                     = "aliyun_3_x64_20G_alibase_20220225.vhd"   //https://help.aliyun.com/document_detail/173054.html?spm=a2c4g.11186623.6.581.6c0571c8KnnMZV
  resource_group_id            = "rg-aek2tc4weboko6i"
  password                     = "Just4Test"
  system_disk_category         = "cloud_essd"
  system_disk_size             = "80"
  install_cloud_monitor        = true
  instance_charge_type         = "PostPaid"
  runtime_name                 = "docker"
  runtime_version              = "19.03.15"
  # # use PrePaid
  # instance_charge_type         = "PrePaid"
  # period                       = 1
  # period_unit                  = "Month"
  # auto_renew                   = true
  # auto_renew_period            = 1
  desired_size                  = 1 //which can be zero
  management {
    auto_repair       = false
    auto_upgrade      = false
    max_unavailable   = 1
  } 
  # # With auto-scaling is enabled, the nodes in the node pool will be labeled with `k8s.aliyun.com=true` to prevent system pods such as coredns, metrics-servers from being scheduled to elastic nodes, and to prevent node shrinkage from causing business abnormalities.
  # scaling_config {
  #   min_size          = 2
  #   max_size          = 10
  #   is_bond_eip       = false
  # }
  labels {
    key               = "env"
    value             = "staging-ack"
  }
  labels {
    key               = "cluster"
    value             = "stg-ack-operation-cluster-cnsz"
  }
  labels {
    key               = "nodePool"
    value             = "public-worker-node-v1-v1-22-v2"
  }
  tags                = local.tags
}
